package com.abby.time;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

public class TimeUtil {

    /**
     * get part of the date info which can be year，month,day an so on
     * 获取部分时间，可以是年月日以及时分秒
     * @param date the date to to be parsed,待解析的时间
     * @param format which format,指定的格式
     * @return the time string，返回指定格式时间字符串
     */
    public static String getPartOfTheDay(Date date,TimeFormat format){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format.getFormat());
        return simpleDateFormat.format(date);
    }

    /**
     * get season of year
     * 获取当前的季节
     * @param date, date to be parsed，待解析的时间
     * @return current season,当前季度
     */
    public static String getSeasonOfYear(Date date){
        String month=getPartOfTheDay(date,TimeFormat.MONTH);
        int m = Integer.parseInt(month);
        if(m<1||m>12){
           return "";
        }
        if(m>=1&&m<=3){
           return "春季";
        }else if(m>=4&&m<=6){
            return "夏季";
        }else if(m>=7&&m<=9){
            return "秋季";
        }else{
            return "冬季";
        }
    }

    /**
     * get day of the year
     * 获取当前是一年中的哪一天
     * @param date, the date to be parsed，待解析的日期
     * @return day of year,一年中的哪一天
     */
    public static int getDayOfYear(Date date){
       LocalDateTime localDateTime=LocalDateTime.ofInstant(date.toInstant(), ZoneId.systemDefault());
       return localDateTime.getDayOfYear();
    }
}
